package com.zzcm.channelinfo.runcommand;

import com.zzcm.channelinfo.control.SilentInstallationControl;
import com.zzcm.channelinfo.tools.Tools;
import android.content.Context;
import android.content.Intent;
import java.util.ArrayList;

public class RunCommand {
    
    private Context context;
    
    public RunCommand(Context context) {
        this.context = context;
    }
    
    public void setContext(Context context) {
        this.context = context;
    }
    
    /**
     * 显示LOG信息
     */
    private String TAG = this.getClass().getName();
    private boolean showLog = true;// true;//false;
    public void showLog(String tag, String msg) {
        if (showLog) {
        	Tools.showLog(TAG,tag+" : "+msg);
        }
    }
    
    public void runCommandUnInit() {
        stopAllThread();
        this.context = null;
    }
    
    private void stopAllThread() {
        stopRunCommandThread();
        stopCheckRunCommandTimeOutThread();
        try {
            Thread.sleep(20);
        } catch(InterruptedException e) {
            
        }
    }
    
    /**
     * 停止执行普通命令
     */
    private void stopRunCommandThread() {
        if(runCommandThread!=null)
        {
            try {
                runCommandThread.destroy();
            } catch (NullPointerException e) {

            }
            runCommandThread = null;
        }
        currentRunCommandList=null;
    }
    
    /**
     * 停止检查当前普通命令行执行是否超时
     */
    private void stopCheckRunCommandTimeOutThread() {
        if(checkRunCommandTimeOutThread!=null)
        {
            try {
            	checkRunCommandTimeOutThread.destroy();
            } catch (NullPointerException e) {

            }
            checkRunCommandTimeOutThread = null;
        }
    }
    
    private Thread checkRunCommandTimeOutThread;
    private int beginRunCommand,timeOutSecondRunCommand=60;
    private ArrayList<String> currentRunCommandList;
    private void startRunCommandTimeOutCheckThread(final String id) {
    	if(checkRunCommandTimeOutThread==null)
    	{
    		checkRunCommandTimeOutThread=new Thread() {
    			//线程是否需要停止的判断标志，默认为false
                private final boolean stopThread[] = {
                        false
                };
                //用于反馈当前线程的运行状态
                @Override
                public boolean isInterrupted() {
                    return stopThread[0];
                }
                //停止当前运行的线程
                @Override
                public void destroy() {
                    stopThread[0] = true;
                }

                @Override
                public void run() {
                	while(!stopThread[0]&&currentRunCommandList!=null)
                	{
                		while(!stopThread[0]&&beginRunCommand<timeOutSecondRunCommand)
                		{
                			try {
                				Thread.sleep(1000);//每秒轮询一次检测是否超时
                            } catch (InterruptedException e1) {
                            }
                			beginRunCommand++;
                		}
                		if(!stopThread[0]&&currentRunCommandList!=null)
                    	{
                			Intent resultIntent = new Intent(SilentInstallationControl.RunCommandParameter.RUN_COMMAND_RESULT);
                            if(id!=null)
                            {
                                resultIntent.putExtra(SilentInstallationControl.RunCommandParameter.ID, id);
                            }
                            resultIntent.putExtra(SilentInstallationControl.RunCommandParameter.RESULT, SilentInstallationControl.RunCommandParameter.RUNCOMMAND_TIMEOUT_RESULT);
                            if(context!=null)
                            {
                                context.sendBroadcast(resultIntent);
                            }
                            currentRunCommandList=null;
                            stopRunCommandThread();
                    	}
                	}
                	if(!stopThread[0])
                	{
                		checkRunCommandTimeOutThread=null;
	                    stopThread[0] = true;
                	}
                }
    		};
    		checkRunCommandTimeOutThread.start();
    	}
    }
    
    private Thread runCommandThread;
    public void runCommand(final String id,final ArrayList<String> commandList,final boolean root) {
        showLog("runCommand", "id="+id+" commandList="+Tools.showArrayList(commandList));
        runCommandThread = new Thread() {
            private final boolean stopThread[] = {
                    false
            };
            
            @Override
            public boolean isInterrupted() {
                return stopThread[0];
            }

            @Override
            public void destroy() {
                stopThread[0] = true;
            }

            @Override
            public void run() {
            	beginRunCommand=0;
                currentRunCommandList=commandList;
                startRunCommandTimeOutCheckThread(id);
                String result="";
                if(!root)
                {
                	result=Tools.runCommand(commandList,stopThread);
                }
                else
                {
                	result=Tools.runRootCommand(commandList,stopThread);
                }
                showLog("runCommand", "id="+id+" result="+result);
                if(!stopThread[0])
                {
                    Intent resultIntent = new Intent(SilentInstallationControl.RunCommandParameter.RUN_COMMAND_RESULT);
                    if(id!=null)
                    {
                        resultIntent.putExtra(SilentInstallationControl.RunCommandParameter.ID, id);
                    }
                    resultIntent.putExtra(SilentInstallationControl.RunCommandParameter.RESULT, result);
                    if(context!=null)
                    {
                        context.sendBroadcast(resultIntent);
                        currentRunCommandList=null;
                    }
                }
            }
        };
        runCommandThread.start();
    }
    
}
